package CCRMClient.GUI;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Component;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientCustomerManagement;
import CCRMServer.DBConnect;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;

import javax.swing.JComboBox;
/**
 * This class implements add customer  screen panel
 * @author Ola
 *
 */
public class AddCustomerScreen extends JPanel {
	
	
	private static final long serialVersionUID = 1L;
	private JLabel lblAddCustomer;
	private JLabel lblCustomerID;
	private JLabel lblCustmerName;
	private JLabel lblCustomerBirthday;
	private JLabel lblYear;
	private JLabel lblMonth;
	private JLabel lblDay;


	private JLabel lblCustomerPhone;
	private JLabel lblCustomerAddress;
	private JLabel lblCustomerRegion;
	private JTextField textCustomerID;
	private JTextField textCustomerName;
	private JTextField textCustomerBirthdayYear;
	private JTextField textCustomerBirthdayMonth;
	private JTextField textCustomerBirthdayDay;
	private JTextField textCustomerPhone;
	private JTextField textCustomerAddress;
	private JTextField textCustomerRegion; 

	private JButton btnAddCustomer;
	private JButton btnCancel;
	
	
	
	/**
	 * Create add customer panel
	 */
	public AddCustomerScreen() {
		
		
		initialize();
	}

	
	private void initialize(){
		setSize (565,549);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		
		
		add(getLblAddCustomer());
		add(getLblCustomerID());
		add(getLblCustomerName());
		add(getLblCustomerBirthday());
		add(getLblCustomerBirthdayYear());
		add(getLblCustomerBirthdayMonth());
		add(getLblCustomerBirthdayDay());
		add(getLblCustomerPhone());
		add(getLblCustomerAddress());	
		add(getLblCustomerRegion());
		add(getTextCustomerID());
		add(getTextCustomerName());
		add(getTextCustomerBirthdayYear());
		add(getTextCustomerBirthdayMonth());
		add(getTextCustomerBirthdayDay());
		add(getTextCustomerPhone());
		add(getTextCustomerAddress());
	
		add(getTextCustomerRegion());
		add(getBtnAddCustomer());
		add(getBtnCancel());
		}
	
	
	JLabel getLblAddCustomer(){
		if(lblAddCustomer==null){
			lblAddCustomer=new JLabel("Add Customer");
			lblAddCustomer.setHorizontalAlignment(SwingConstants.CENTER);
			lblAddCustomer.setForeground(Color.RED);
			lblAddCustomer.setFont(new Font("David", Font.BOLD, 26));
			lblAddCustomer.setBackground(Color.WHITE);
			lblAddCustomer.setBounds(0, 13, 565, 39);
		}
		return lblAddCustomer;
		
		
	}
	
	JLabel getLblCustomerID(){
		
		if(lblCustomerID==null){
			lblCustomerID=new JLabel("Customer ID");
			lblCustomerID.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerID.setBounds(30, 63, 164, 29);
		}
		return lblCustomerID;
		
	}
	
	JLabel getLblCustomerName(){
		if(lblCustmerName==null){
			lblCustmerName= new JLabel("Customer Name");
			lblCustmerName.setFont(new Font("David", Font.BOLD, 20));
			lblCustmerName.setBounds(30, 107, 164, 29);
		}
			
		
		return lblCustmerName;
		
	}
	JLabel getLblCustomerBirthday() {
		if(lblCustomerBirthday== null){
			lblCustomerBirthday=new JLabel("Customer Birthday :");
			lblCustomerBirthday.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerBirthday.setBounds(30, 148, 179, 29);
		}
		return lblCustomerBirthday;
	}
	
	JLabel getLblCustomerBirthdayYear() {
		if(lblYear== null){
			lblYear=new JLabel("Year");
			lblYear.setFont(new Font("David", Font.BOLD, 20));
			lblYear.setBounds(30, 180, 179, 29);
		}
		return lblYear;
	}
	
	JLabel getLblCustomerBirthdayMonth() {
		if(lblMonth== null){
			lblMonth=new JLabel("Month");
			lblMonth.setFont(new Font("David", Font.BOLD, 20));
			lblMonth.setBounds(30, 222, 179, 29);
		}
		return lblMonth;
	}
	
	JLabel getLblCustomerBirthdayDay() {
		if(lblDay== null){
			lblDay=new JLabel("Day");
			lblDay.setFont(new Font("David", Font.BOLD, 20));
			lblDay.setBounds(30, 267, 179, 29);
		}
		return lblDay;
	}
	

	JLabel getLblCustomerPhone(){
		
	
		if(lblCustomerPhone == null){
			lblCustomerPhone=new JLabel("Customer Phone");
			lblCustomerPhone.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerPhone.setBounds(30, 347, 164, 29);
		}
		return lblCustomerPhone;
		
	}
	

	JLabel getLblCustomerAddress() {
		if(lblCustomerAddress == null){
			lblCustomerAddress=new JLabel("Customer Address");
			lblCustomerAddress.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerAddress.setBounds(30, 307, 164, 29);
		}
		
		return lblCustomerAddress;
	}
	
	

	
	JLabel getLblCustomerRegion() {
		if(lblCustomerRegion == null){
			lblCustomerRegion=new JLabel("Customer Region");
			lblCustomerRegion.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerRegion.setBounds(30, 387, 164, 29);
			}
		
		
		return lblCustomerRegion ;
	}

	JTextField getTextCustomerID() {
		if (textCustomerID == null) {
			textCustomerID = new JTextField();
			textCustomerID.setColumns(10);
			textCustomerID.setBackground(SystemColor.menu);
			textCustomerID.setBounds(237, 63, 251, 20);
		}
		
		return textCustomerID ;
	}
	JTextField getTextCustomerName() {
		if (textCustomerName == null) {
			textCustomerName = new JTextField();
			textCustomerName.setColumns(10);
			textCustomerName.setBackground(SystemColor.menu);
			textCustomerName.setBounds(237, 112, 251, 20);
		}
		
		
		return textCustomerName;
	}
	
	JTextField getTextCustomerBirthdayYear() {
		if (textCustomerBirthdayYear == null) {
			textCustomerBirthdayYear = new JTextField();
			textCustomerBirthdayYear.setColumns(10);
			textCustomerBirthdayYear.setBackground(SystemColor.menu);
			textCustomerBirthdayYear.setBounds(237, 185, 155, 20);
		}
		
		return textCustomerBirthdayYear;
	}
	
	JTextField getTextCustomerBirthdayMonth() {
		if (textCustomerBirthdayMonth == null) {
			textCustomerBirthdayMonth = new JTextField();
			textCustomerBirthdayMonth.setColumns(10);
			textCustomerBirthdayMonth.setBackground(SystemColor.menu);
			textCustomerBirthdayMonth.setBounds(237, 227, 155, 20);
		}
		
		return textCustomerBirthdayMonth;
	}
	
	JTextField getTextCustomerBirthdayDay() {
		if (textCustomerBirthdayDay == null) {
			textCustomerBirthdayDay = new JTextField();
			textCustomerBirthdayDay.setColumns(10);
			textCustomerBirthdayDay.setBackground(SystemColor.menu);
			textCustomerBirthdayDay.setBounds(237, 270, 155, 20);
		}
		
		return textCustomerBirthdayDay;
	}
	
	
	
	JTextField getTextCustomerPhone() {
		if (textCustomerPhone == null) {
			textCustomerPhone = new JTextField();
			textCustomerPhone.setColumns(10);
			textCustomerPhone.setBackground(SystemColor.menu);
			textCustomerPhone.setBounds(237, 352, 251, 20);
		}
		return textCustomerPhone;
	}
	
	JTextField getTextCustomerAddress() {
		if (textCustomerAddress == null) {
			textCustomerAddress = new JTextField();
			textCustomerAddress.setColumns(10);
			textCustomerAddress.setBackground(SystemColor.menu);
			textCustomerAddress.setBounds(237, 312, 251, 20);
		}
		
		return textCustomerAddress;
	}
	JTextField getTextCustomerRegion() {
		if (textCustomerRegion == null) {
			textCustomerRegion = new JTextField();
			textCustomerRegion.setColumns(10);
			textCustomerRegion.setBackground(SystemColor.menu);
			textCustomerRegion.setBounds(237, 390, 251, 20);
		}
		
		return textCustomerRegion;
	}
	
	
	
	JButton getBtnAddCustomer(){
		if (btnAddCustomer == null) {
			btnAddCustomer = new JButton("Add New Customer");
			btnAddCustomer.addActionListener(new BntAddCustomerActionListener());
			btnAddCustomer.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnAddCustomer.setBounds(49, 460, 195, 23);
		}
		return btnAddCustomer;
		
	}
	
	JButton getBtnCancel(){
		if (btnCancel == null) {
			btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new BtnCancelActionListener());
			btnCancel.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCancel.setBounds(289, 460, 230, 23);
	}
		return btnCancel;
		
	}
	/**
	 * This inner class implements event listener for add search button
	 * @author Ola
	 *
	 */
	 private class BntAddCustomerActionListener implements ActionListener{
		 public void actionPerformed(ActionEvent arg0) {
			try {
				String CustomerID=textCustomerID.getText();
				String CustomerName=textCustomerName.getText();
				String customerYear = textCustomerBirthdayYear.getText();
				String customerMonth = textCustomerBirthdayMonth.getText();
				String customerDay = textCustomerBirthdayDay.getText();
				int year = 0;
				int month = 0;
				int day = 0;
				Date customerBrithday = null;
				try {
					year = Integer.parseInt(customerYear);
					if (year < 1895 || year > 2001) {
						JOptionPane.showMessageDialog(null, "Invalid year input", "Add customer", JOptionPane.INFORMATION_MESSAGE);
						textCustomerBirthdayYear.setText("");
					}
					month = Integer.parseInt(customerMonth);
					if (month < 1 || month > 12) {
						JOptionPane.showMessageDialog(null, "Invalid month input", "Add customer", JOptionPane.INFORMATION_MESSAGE);
						textCustomerBirthdayMonth.setText("");
					} else if (month < 10)
						customerMonth = "0" + Integer.toString(month);
					day = Integer.parseInt(customerDay);
					if (day < 1 || day > 31) {
						JOptionPane.showMessageDialog(null, "Invalid day input", "Add customer", JOptionPane.INFORMATION_MESSAGE);
						textCustomerBirthdayDay.setText("");
					} else if (day < 10)
						customerDay = "0" + Integer.toString(day);
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Birthday fields must be numbers", "Add customer", JOptionPane.INFORMATION_MESSAGE);
					textCustomerBirthdayYear.setText("");
					textCustomerBirthdayMonth.setText("");
					textCustomerBirthdayDay.setText("");
					throw new NullPointerException();
				}
                    String str_date= customerYear + "-" + customerMonth + "-" + customerDay;
                    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    try {
						customerBrithday = (Date) formatter.parse(str_date);
					} catch (ParseException e2) {
						JOptionPane.showMessageDialog(null, "Invalid date input", "Add customer", JOptionPane.INFORMATION_MESSAGE);
						textCustomerBirthdayYear.setText("");
						textCustomerBirthdayMonth.setText("");
						textCustomerBirthdayDay.setText("");
						throw new NullPointerException();
					}
                    String customerPhone=textCustomerPhone.getText();
					String customerAddress=textCustomerAddress.getText();
					int customerStatus = 1;
					int regionID = 0;
					String region = textCustomerRegion.getText();
					try {
						regionID = Integer.parseInt(region);
					} catch (NumberFormatException e3) {
						JOptionPane.showMessageDialog(null, "Invalid region ID input", "Add customer", JOptionPane.INFORMATION_MESSAGE);
						textCustomerRegion.setText("");
						throw new NullPointerException();
					}
					if (CustomerID.isEmpty()||CustomerName.isEmpty()|| customerPhone.isEmpty()||customerAddress.isEmpty()) throw new NullPointerException();
					try {
						Customer cust= new Customer(CustomerID,CustomerName,customerBrithday,customerPhone,customerAddress,customerStatus,regionID);
						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_CUSTOMER, cust);
			 	       } catch (IOException e1) {
			 	    	   JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
			 	    	   ClientStart.quitApp();
			 	       }

			} catch (NullPointerException e) {
        	  JOptionPane.showMessageDialog(null, "All fields are mandatory", "Add customer problem", JOptionPane.INFORMATION_MESSAGE);
			}
		 }
	 }	
	 
	 /**
	  * This inner class implements event listener for add cancel button
	  * @author Ola
	  *
	  */
	private class BtnCancelActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CustomerManage());
		}
  }
	 }
